<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="租赁人" prop="leaseholder">
              <el-input v-model="queryParams.leaseholder" placeholder="请输入租赁人" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="联系方式" prop="contactInformation">
              <el-input v-model="queryParams.contactInformation" placeholder="请输入联系方式" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="名称" prop="propertyName">
              <el-input v-model="queryParams.propertyName" placeholder="请输入名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
            <el-form-item label="退租时间" prop="startDate">
              <el-date-picker v-model="queryParams.startDate" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择开始日期">
              </el-date-picker>
              <el-date-picker
                v-model="queryParams.endDate"
                clearable
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择结束日期"
                class="ml-5"
              ></el-date-picker>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <div style="display: flex; justify-content: flex-end; align-items: center">
          <el-button type="warning" icon="Download" @click="handleExport">导出</el-button>
          <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()">删除</el-button>
        </div>
      </template>

      <el-table v-loading="loading" :data="leaseList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="false" label="主键" align="center" prop="id" />
        <el-table-column v-if="false" label="房产基础信息主键" align="center" prop="basicPropertyInformationId" />
        <el-table-column v-if="false" label="房产租赁主键" align="center" prop="houseRentalId" />
        <el-table-column label="租赁人" align="center" prop="leaseholder" />
        <el-table-column label="联系方式" align="center" prop="contactInformation" />
        <el-table-column label="开始时间" align="center" prop="startTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center" prop="endTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="退租时间" align="center" prop="rentOutTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.rentOutTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" align="center" prop="projectName" />
        <el-table-column label="楼栋号" align="center" prop="towerNumber" />
        <el-table-column label="所在楼层" align="center" prop="floor" />
        <el-table-column label="房间号" align="center" prop="roomNumber" />
        <el-table-column label="建筑面积(㎡)" align="center" prop="floorArea" />
        <el-table-column label="退租类型" align="center" prop="rentOutType">
          <template #default="scope">
            <dict-tag :options="park_surrender_tenancy_status" :value="scope.row.rentOutType" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="详情" placement="top">
              <el-button link type="primary" @click="handleView(scope.row.basicPropertyInformationId, scope.row.houseRentalId)">详情</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>

    <HouseFormDialog ref="houseFormDialogRef" @refresh="getList" />
  </div>
</template>

<script setup name="Lease" lang="ts">
import { listLease, getLease, delLease, addLease, updateLease } from '@/api/zichanguanli/hosuehistoricallease/index';
import { LeaseVO, LeaseQuery, LeaseForm } from '@/api/zichanguanli/hosuehistoricallease/types';
import HouseFormDialog from '@/components/HouseFormDialog/index.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { park_surrender_tenancy_status } = toRefs<any>(proxy?.useDict('park_surrender_tenancy_status'));

const leaseList = ref<LeaseVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const leaseFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const houseFormDialogRef = ref(null);
const houseFormDialogOpen = (type: string, basicPropertyInformationId?: number | string, houseRentalId?: number | string) => {
  houseFormDialogRef.value.open(type, basicPropertyInformationId, houseRentalId);
};
const initFormData: LeaseForm = {};
const data = reactive<PageData<LeaseForm, LeaseQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    leaseholder: undefined,
    propertyName: undefined,
    rentOutType: undefined,
    startDate: undefined
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询房产历史租赁列表 */
const getList = async () => {
  loading.value = true;
  const res = await listLease(queryParams.value);
  leaseList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  (queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    leaseholder: undefined,
    contactInformation: undefined,
    endDate: undefined,
    propertyName: undefined,
    rentOutType: undefined,
    startDate: undefined
  }),
    getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: LeaseVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  houseFormDialogOpen('create');
};

/** 修改按钮操作 */
const handleUpdate = async (row) => {
  const _id = row?.id || ids.value[0];
  houseFormDialogOpen('edit', _id);
};

/** 删除按钮操作 */
const handleDelete = async (row?: LeaseVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('确定删除所选的信息吗？').finally(() => (loading.value = false));
  await delLease(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

const handleView = async (basicPropertyInformationId?: number | string, houseRentalId?: number | string) => {
  houseFormDialogOpen('history', basicPropertyInformationId, houseRentalId);
};
/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('zichanguanli/lease/export', { ...queryParams.value }, `lease_${new Date().getTime()}.xlsx`);
};

onMounted(() => {
  getList();
});
</script>
